import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

/**
 * 读取Android最新的xml，转换成H5的格式
 * Created by heyiyong on 2016/5/18.
 */
public class AndroidXmlToIosProperties {
    public static final String fileName = "xml/latestFile.xml";
    public static final String saveFileName = "sort_properties.properties";
    public static Map<String, String> keyValMap = new TreeMap<>();


    public static void main(String[] args) throws Exception {
        // 1、找到文件源
        URL resource = XmlSorter.class.getResource(fileName);
        File file = new File(resource.getFile());

        // 2、读取文件，加入到treeMap里
        readXml(file);

        // 3、写出property文件
        File saveFile = new File(file.getParent(), saveFileName);
        Utils.writeProperties(keyValMap.entrySet(), saveFile);
    }

    private static void readXml(File file) throws Exception {
        SAXReader reader = new SAXReader();
        Document document = reader.read(file);
        Element root = document.getRootElement();
        Iterator<Element> iterator = root.elementIterator();
        while (iterator.hasNext()) {
            Element element = iterator.next();
            String name = element.attribute("name").getValue();
            String value = element.getStringValue();
//            System.out.println("name = " + name + ", text = " + element.getTextTrim());
            value = Utils.filter(value);
            keyValMap.put(name, value);
        }
    }


}
